'use strict';

const Controller = require('egg').Controller;

class CommentController extends Controller {
  /**
     *
     * @api {POST} /api/comment/create 客户端-创建评论
     * @apiName 创建评论
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} blogId 博客id
     * @apiParam  {Number} projectId 项目id
     * @apiParam  {Number} parentCommentId 父评论id(不是回复另一条评论可不填)
     * @apiParam  {String} content description
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "blogId":58,
            "projectId":4,
            "content":"评论7",
            "parentCommentId":7
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "likeCount": 0,                 //评论点赞次数
                "id": 15,                       //评论id
                "userId": 17,                   //评论的用户的id
                "userName": "mojito",           //评论的用户的昵称
                "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //评论的用户的头像
                "beRepliedUserId": 16,                  //被回复的用户的id
                "beRepliedUserName": "mojito",          //被回复的用户的昵称
                "beRepliedUserAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",  被回复的用户的头像
                "blogId": "58",                         //博客id
                "content": "回复16",                    //评论的内容
                "authorId": 20,                         //博客作者id
                "parentCommentId": "14",                //父评论id
                "projectId": "4",                       //项目id
                "updatedAt": "2021-10-11T07:10:52.361Z",
                "createdAt": "2021-10-11T07:10:52.361Z"     //创建时间
            }
        }
     *
     *
     */
  // 评论
  async create() {
    const { ctx, service } = this;
    let checkRes = '';

    // 判断是否传入父评论id 决定校验规则
    if (ctx.request.body.parentCommentId) {
      checkRes = new this.ctx.helper.checkData(
        this,
        'blogId',
        'content',
        'parentCommentId',
        'projectId'
      ).checkDataRes;
    } else {
      checkRes = new this.ctx.helper.checkData(
        this,
        'blogId',
        'content',
        'projectId'
      ).checkDataRes;
    }
    try {
      if (checkRes) {
        const res = await service.blog.comment.create();
        if (res === '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res === '文章不存在') {
          return ctx.helper.errorModule(res);
        }
        if (res === '该评论不存在') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('评论失败');
    }
  }
  /**
     *
     * @api {GET} /api/comment/getMyComment?projectId=4&limit=10&offset=0 客户端-获取自己创建的评论(token)
     * @apiName 获取自己创建的评论
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {Number} projectId 项目id
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 1,     //评论总数
                "rows": [
                    {
                        "id": 15,                       //评论id
                        "userId": 17,                   //评论的用户id
                        "userName": "mojito",           //评论的用户昵称
                        "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //评论的用户头像
                        "beRepliedUserId": 16,          //此评论回复的用户的id
                        "beRepliedUserName": "mojito",  //此评论回复的用户的昵称
                        "beRepliedUserAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",  //此评论回复的用户的头像
                        "blogId": 58,                   //博客id
                        "authorId": 20,                 //作者id
                        "content": "回复16",            //评论内容
                        "parentCommentId": 14,          //父评论id
                        "likeCount": 0,                 //点赞次数
                        "createdAt": "2021-10-11T07:10:52.000Z",    //创建时间
                        "updatedAt": "2021-10-11T07:10:52.000Z",
                        "deletedAt": null
                    }
                ]
            }
        }
     *
     *
     */
  // 获取自己创建的评论
  async getMyComment() {
    const { app, ctx, service } = this;

    const res = await service.blog.comment.getMyComment();
    if (res == '项目id错误') {
      return ctx.helper.errorModule(res);
    }
    return ctx.helper.successModule(res);
  }
  /**
     *
     * @api {GET} /api/comment/getMyReply?projectId=4&limit=10&offset=0 客户端-获取我的评论回复(token)
     * @apiName 获取我的评论回复
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {Number} projectId 项目id
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 1,             //回复总数
                "rows": [
                    {
                        "id": 15,                   //评论id
                        "userId": 17,               //创建评论用户id
                        "userName": "mojito",       //创建评论用户昵称
                        "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //创建评论用户头像
                        "beRepliedUserId": 16,          //被回复的用户的id
                        "beRepliedUserName": "mojito",  //被回复的用户的昵称
                        "beRepliedUserAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",  //被回复的用户的头像
                        "blogId": 58,                   //博客id
                        "authorId": 20,                 //博客作者id
                        "content": "回复16",            //评论内容
                        "parentCommentId": 14,          //父评论id
                        "likeCount": 0,                 //点赞次数
                        "createdAt": "2021-10-11T07:10:52.000Z",        //创建时间
                        "updatedAt": "2021-10-11T07:10:52.000Z",
                        "deletedAt": null
                    }
                ]
            }
        }
     *
     *
     */
  // 获取我的回复
  async getMyReply() {
    const { app, ctx, service } = this;

    // 如果有缺少参数

    const res = await service.blog.comment.getMyReply();
    if (res == '项目id错误') {
      return ctx.helper.errorModule(res);
    }
    return ctx.helper.successModule(res);
  }
  /**
     *
     * @api {GET} /api/comment/get?id=62&projectId=4&limit=10&offset=0 客户端-获取当前博客评论
     * @apiName 获取当前博客评论
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 博客id
     * @apiParam  {Number} projectId 项目id
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 1,         //博客评论总数
                "rows": [
                    {
                        "id": 17,               //评论id
                        "userId": 29,           //评论的用户id
                        "userName": "mojito",   //评论的用户昵称
                        "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //评论的用户头像
                        "beRepliedUserId": null,        //该评论回复的用户的id
                        "beRepliedUserName": null,      //该评论回复的用户的昵称
                        "beRepliedUserAvatar": null,    //该评论回复的用户的头像
                        "blogId": 62,                   //博客id
                        "projectId": 4,                 //项目id
                        "authorId": 28,                 //博客作者id
                        "content": "test",              //评论内容
                        "parentCommentId": 0,           //父评论id(0说明不是回复别人的评论)
                        "likeCount": 0,                 //评论点赞次数
                        "createdAt": "2021-10-13T01:58:05.000Z",        //评论创建时间
                        "updatedAt": "2021-10-13T01:58:05.000Z",
                        "deletedAt": null
                    }
                ]
            }
        }
     *
     *
     */
  // 获取当前博客评论
  async get() {
    const { app, ctx, service } = this;
    const checkRes = new this.ctx.helper.checkData(this, 'id').checkDataRes;
    // 如果有缺少参数
    if (checkRes) {
      const res = await service.blog.comment.get();
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    }
  }

  /**
     *
     * @api {DELETE} /api/comment/delete 删除评论(管理员 博客作者 评论的用户才可以删)
     * @apiName 删除评论
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 评论id
     * @apiParam  {Number} beRepliedUserId 被回复的用户id(如果该评论不是回复另一条评论可不填)
     * @apiParam  {Number} projectId 项目id
     *
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  // 删除评论(管理员 博客作者 评论的人)
  async delete() {
    const { app, ctx, service } = this;
    let checkRes = '';
    try {
      // 判断校验规则
      if (ctx.request.body.beRepliedUserId) {
        checkRes = new this.ctx.helper.checkData(
          this,
          'id',
          'projectId',
          'beRepliedUserId'
        ).checkDataRes;
      } else {
        checkRes = new this.ctx.helper.checkData(this, 'id', 'projectId')
          .checkDataRes;
      }
      if (checkRes) {
        const res = await service.blog.comment.delete();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '评论不存在') {
          return ctx.helper.errorModule(res);
        }
        if (res == '删除失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('删除失败');
    }
  }
  /**
     *
     * @api {POST} /api/comment/like 客户端-点赞/取消点赞评论(需要token)
     * @apiName 点赞/取消点赞评论
     * @apiGroup 评论模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} commentId 评论id
     * @apiParam  {Number} blogId 博客id
     * @apiParam  {Number} projectId 项目id
     *
     * @apiParamExample  {type} Request-Example:
        {
            "commentId":22,
            "blogId":64,
            "projectId":4
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "点赞成功/取消点赞成功",
            "data": []
        }
     *
     *
     */
  // 点赞/取消点赞评论
  async like() {
    const { app, ctx, service } = this;
    try {
      const checkRes = new ctx.helper.checkData(
        this,
        'commentId',
        'blogId',
        'projectId'
      ).checkDataRes;
      // 如果有缺少参数
      if (checkRes) {
        const res = await service.blog.comment.like();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '该评论不存在') {
          return ctx.helper.errorModule(res);
        }
        if (res == '点赞成功') {
          return ctx.helper.successModule('点赞成功');
        }
        return ctx.helper.successModule('取消点赞成功');
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }
}
module.exports = CommentController;
